*This do file creates the file containing the information about conflicts

cd "/Users/christopherrauh/Dropbox/JEEA_replication/"

clear
use country_id_isocode_crosswalk.dta
levelsof isocode, local(levels) 


clear all
insheet using ucdp_grid.csv




sum year 
local bigy = `r(max)'
sum month if year == `bigy'
local bigm = `r(max)'


foreach z of local levels {
	*create first month
	local new = _N + 1
	set obs `new'
	replace isocode = "`z'" if _n == _N
	replace year = 1989 if _n == _N
	replace month = 1 if _n == _N
	foreach k in best ged_best_ns ged_best_os ged_best_sb {
		replace `k' = 0 if _n == _N
	}
	
	*create last month
	local new = _N + 1
	set obs `new'
	replace isocode = "`z'" if _n == _N
	replace year = `bigy' if _n == _N
	replace month = `bigm' if _n == _N
	foreach k in best ged_best_ns ged_best_os ged_best_sb {
		replace `k' = 0 if _n == _N
	}
}

collapse (max) best ged_best_ns ged_best_os ged_best_sb, by(isocode year month)


generate ym=ym(year, month)
egen country_id = group(isocode)

sort country_id ym
tsset country_id ym
tsfill
replace month = month(dofm(ym))  if month  == .
replace year = yofd(dofm(ym)) if year == .

bysort country_id: carryforward isocode, gen(yn)
replace isocode = yn if isocode == ""
drop yn
foreach k in best ged_best_ns ged_best_os ged_best_sb {
	replace `k' = 0 if  `k' == .
}
keep isocode  best year month ged_best_ns ged_best_os ged_best_sb



sort isocode year month

merge isocode year month using isocode_war_info
drop _merge

replace best=1 if armed==1 & year<1989
replace best=80 if war==1 & year<1989
replace best=0 if best==. & year<1989


generate anyviolence = best>0
replace anyviolence=. if best==.
generate armedconf = best>49
replace armedconf=. if best==.
generate civilwar = best>499
replace civilwar=. if best==.

foreach j in ged_best_ns ged_best_os ged_best_sb {
	replace `j' = 1 if `j' >= 1 & `j' != .
}

egen countryid = group(isocode)

gen ym = ym(year,month)
tsset countryid ym

*make predictor of cumulative best for last months
local g0 3
local g0 = `g0' + 1
foreach g in 6 12 60 120 {
	gen past`g' = 0
	forval f = `g0' / `g' {
		replace past`g' = past`g' + L`f'.best if L`f'.best != .
	}
	local `g0' = `g' + 1
}



foreach k in anyviolence armedconf civilwar ged_best_ns ged_best_os ged_best_sb {
	tsspell `k'
	replace _seq = 0 if `k' == 1
	rename _seq `k'_dp
	drop _end _spell
	
}


*onset
foreach k in anyviolence armedconf civilwar ged_best_ns ged_best_os ged_best_sb {
	
	foreach j in 1 3 12 {
		gen ons_`k'`j'= 0
		forval v = 1 / `j' {
			replace ons_`k'`j' = 1 if F`v'.`k' == 1
		}
		replace ons_`k'`j' = . if `k' == 1
		replace ons_`k'`j' = . if `k' == .
		replace ons_`k'`j' = . if F1.`k' == .
	}
}

*incidence (only one month ahead)
foreach k in anyviolence armedconf civilwar {
	
	*foreach j in 1 3 12 {
	foreach j in 1 {
		gen `k'`j'= 0
		forval v = 1 / `j' {
			replace `k'`j' = 1 if F`v'.`k' == 1
		}
		*replace `k'`j' = . if `k' == 1
		replace `k'`j' = . if `k' == .
		replace `k'`j' = . if F1.`k' == .
	}
}

*drop if countryid == .


sort isocode year month

save masterfile_CR, replace
************

***********
*now check for contingent conflicts

clear
use masterfile_CR


drop if best == .

keep isocode country year month best

save "tempviol.dta", replace


use "dyadic distance.dta", clear



rename iso_d isocode

*YUG values for serbia and montenegro
replace isocode="SRB" if isocode=="YUG"
replace iso_o="SRB" if iso_o=="YUG"

*SRB is serbia and montenegro
*MNE is Montenegro we let this drop - contiguity is really complicated here
*YUG values for serbia and montenegro


replace isocode="ROU" if isocode=="ROM"
replace iso_o="ROU" if iso_o=="ROM"

replace isocode="COD" if isocode=="ZAR"
replace iso_o="COD" if iso_o=="ZAR"

save temp.dta, replace
keep if isocode=="SDN" | iso_o=="SDN"

replace isocode="SSD" if isocode=="SDN"
replace iso_o="SSD" if iso_o=="SDN"

append using temp.dta
erase temp.dta

merge m:m isocode using "tempviol.dta"

drop if _merge!=3
drop _merge
fillin isocode iso_o year month
bysort isocode iso_o: egen m_contig = mean(contig)
bysort isocode iso_o: egen m_distcap = mean(distcap)

keep m_contig m_distcap isocode iso_o year month
merge m:1 isocode year month using "tempviol.dta"
drop if _merge!=3
drop _merge

rename m_contig contig
rename m_distcap distcap
generate close = distcap<1000
replace contig = 0 if isocode==iso_o
replace close = 0 if isocode==iso_o

*generate variables
generate anyviolence=best>0
replace anyviolence=. if best==.

generate best25=best>24
replace best25=. if best==.

generate best50= best>49
replace best50=. if best==.

generate best500= best>499
replace best500=. if best==.

foreach var of varlist anyviolence  best50 best500 {
	generate contig_`var' = contig*`var'
	generate close_`var' = close*`var'
}
collapse (sum) close contig contig_anyviolence-close_best500, by(iso_o year month)

rename iso_o isocode
sort isocode year month
save contiviolence_monthly.dta, replace

erase tempviol.dta

clear
use masterfile_CR
sort isocode year month 
merge 1:1 isocode year month using contiviolence_monthly, keepusing(contig_anyviolence close_best50 close_best500)
drop if _merge == 2
drop _merge 
sort isocode year month 
save masterfile_CR, replace

erase contiviolence_monthly.dta
